package com.say.framework.core.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.say.bean.domain.Config;
import com.say.service.ConfigService;

@Component
public class SystemResourceLocator implements ApplicationContextAware {

    private static ApplicationContext context;

    private static SystemResourceLocator instance = new SystemResourceLocator();

    @Override
    public void setApplicationContext(ApplicationContext contex) throws BeansException{
        SystemResourceLocator.context = contex;
    }
    
    public static final SystemResourceLocator getInstance() {
        return instance;
    }

    public synchronized static String getValue(String key) {
    	ConfigService configService=getBean(ConfigService.class);
        return configService.getCacheConfigByCode(key);
    }

    public synchronized static void setValue(String code, String value,String desc) {
    	ConfigService configService=getBean(ConfigService.class);
    	Config config=new Config();
    	config.setCode(code);
    	config.setValue(value);
    	config.setDesc(desc);
		configService.saveConfig(config);
    }

    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }
    
    public static <T> T getBean(Class<T> requiredType) {
        return context.getBean(requiredType);
    }
    
}
